package com.universe.titan.controller;


import com.universe.titan.common.vo.Result;
import com.universe.titan.service.UserService;
import com.universe.titan.vo.UserReqVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zeki
 */

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 获取用户列表
     * @param req
     * @return
     */
    @PostMapping("/getUserList")
    public Result getUserList(@RequestBody UserReqVo req){
        try {
            return Result.returnSuccess(userService.selectPageByUserVo(req));
        }catch (Exception e){
            log.error("获取列表有误",e);
            return Result.returnFail("获取列表有误",e.getMessage());
        }
    }

    @PostMapping("/getUserListTest")
    public Result getUserListTest( UserReqVo req){
        try {
            return Result.returnSuccess(userService.selectPageByUserVo(req));
        }catch (Exception e){
            log.error("获取列表有误",e);
            return Result.returnFail("获取列表有误",e.getMessage());
        }
    }

    /**
     * 新增/更新用户
     * @param req
     * @return
     */
    @PostMapping("/saveOrUpdateUser")
    public Result saveOrUpdateUser(@RequestBody UserReqVo req){
        try {
            userService.saveOrUpdateUser(req);
            return Result.returnSuccess("操作成功");
        }catch (Exception e){
            log.error("操作失败",e);
            return Result.returnFail("操作失败",e.getMessage());
        }
    }

    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestBody UserReqVo req){
        try {
            userService.deleteUser(req);
            return Result.returnSuccess("删除成功");
        }catch (Exception e){
            log.error("删除用户失败",e);
            return Result.returnFail("删除用户失败",e.getMessage());
        }
    }



}
